/**
 * 数据血缘进度显示组件
 * 独立处理Dynamic Island进度显示，不影响数据血缘图渲染
 */

import React, { useCallback } from 'react';
import { useDataLineageProgress } from '../hooks/useDataLineageProgress';

interface DataLineageProgressDisplayProps {
  onAnalysisStart?: (notificationId: string) => void;
  onAnalysisUpdate?: (notificationId: string, progress: number, message?: string) => void;
  onAnalysisComplete?: (notificationId: string, success: boolean, data?: any) => void;
}

const DataLineageProgressDisplay: React.FC<DataLineageProgressDisplayProps> = ({
  onAnalysisStart,
  onAnalysisUpdate,
  onAnalysisComplete,
}) => {
  const {
    startLineageAnalysis,
    updateAnalysisProgress,
    completeAnalysis,
    failAnalysis,
  } = useDataLineageProgress();

  // 启动分析进度显示
  const startProgress = useCallback((dataSource: string) => {
    const notificationId = startLineageAnalysis(dataSource);
    onAnalysisStart?.(notificationId);
    return notificationId;
  }, [startLineageAnalysis, onAnalysisStart]);

  // 更新进度
  const updateProgress = useCallback((
    notificationId: string,
    progress: number,
    message?: string,
    statistics?: any
  ) => {
    updateAnalysisProgress(notificationId, progress, message || '', '', statistics);
    onAnalysisUpdate?.(notificationId, progress, message);
  }, [updateAnalysisProgress, onAnalysisUpdate]);

  // 完成分析
  const completeAnalysisProgress = useCallback((
    notificationId: string,
    success: boolean,
    summary?: string,
    statistics?: any,
    data?: any
  ) => {
    if (success) {
      completeAnalysis(notificationId, summary, statistics, data);
    } else {
      failAnalysis(notificationId, { message: summary || '分析失败' });
    }
    onAnalysisComplete?.(notificationId, success, data);
  }, [completeAnalysis, failAnalysis, onAnalysisComplete]);

  // 这个组件不渲染任何UI，只提供进度管理功能
  return null;
};

export default DataLineageProgressDisplay;

// 导出进度管理hooks供外部使用
export const useDataLineageProgressManager = () => {
  const {
    startLineageAnalysis,
    updateAnalysisProgress,
    completeAnalysis,
    failAnalysis,
  } = useDataLineageProgress();

  const startProgress = useCallback((dataSource: string) => {
    return startLineageAnalysis(dataSource);
  }, [startLineageAnalysis]);

  const updateProgress = useCallback((
    notificationId: string,
    progress: number,
    message?: string,
    statistics?: any
  ) => {
    updateAnalysisProgress(notificationId, progress, message || '', '', statistics);
  }, [updateAnalysisProgress]);

  const completeAnalysisProgress = useCallback((
    notificationId: string,
    success: boolean,
    summary?: string,
    statistics?: any,
    data?: any
  ) => {
    if (success) {
      completeAnalysis(notificationId, success, summary, statistics);
    } else {
      failAnalysis(notificationId, { message: summary || '分析失败' });
    }
  }, [completeAnalysis, failAnalysis]);

  return {
    startProgress,
    updateAnalysisProgress: updateProgress,
    completeAnalysisProgress,
  };
};
